#!/bin/sh

msg() {
    ALL_OFF="\e[1;0m"
    BOLD="\e[1;1m"
    GREEN="${BOLD}\e[1;32m"
    local mesg=$1; shift
    printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
    PACMAN_OLD="$(date +%m%d%M%S)"
    # one time stuff for md5sum issue with older pacman versions
    if [ "$(vercmp $2 3.0.2)" -lt 0 ]; then
        _resetbackups
    fi
    if [ ! -f "/etc/pacman.d/gnupg/pubring.gpg" ] || [ "$(vercmp $2 4.0.3-3)" -lt 0 ]; then
        _check_pubring
    fi
    if [ "$(vercmp $2 4.1.0-2)" -lt 0 ]; then
        msg "You had an old pacman.conf. We updated it for you."
        msg "You can find your old pacman.conf saved as \`/etc/pacman-old-${PACMAN_OLD}.conf\`"
        mv /etc/pacman.conf /etc/pacman-old-${PACMAN_OLD}.conf
        mv /etc/pacman.conf.pacnew /etc/pacman.conf
        if [ ! -f "usr/bin/pacman-mirrors" ]; then
           msg "Your current mirrorlist was saved as \`/etc/pacman.d/mirrorlist-old-${PACMAN_OLD}\`"
           msg "A generic mirrorlist, set to our default server, was generated!"
           msg "Use \`pacman-mirrors\` to generate and update your pacman mirrorlist."
           mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist-old-${PACMAN_OLD}
           echo '# file generated due pacman update' > /etc/pacman.d/mirrorlist
           echo 'Server = http://mirror.dacentec.com/manjaro/stable/$repo/$arch' >> /etc/pacman.d/mirrorlist
        fi
    fi
    if [ "x`cat /etc/pacman.conf | grep manjaro-system`" == "x" ]; then
        sed -i -e 's|^.*HoldPkg.*|HoldPkg      = pacman glibc manjaro-system|g' /etc/pacman.conf
        sed -i -e 's|^.*SyncFirst.*|SyncFirst    = manjaro-system|g' /etc/pacman.conf
        msg "Added manjaro-system to SyncFirst line in \`/etc/pacman.conf\`"
    fi
    if [ "x`cat /etc/pacman.conf | grep SyncFirst | cut -d= -f2 | grep pacman`" != "x" ]; then
        sed -i -e 's|^.*SyncFirst.*|SyncFirst    = manjaro-system|g' /etc/pacman.conf
        msg "Removed pacman from SyncFirst line in \`/etc/pacman.conf\`"
    fi
    if [ "$(vercmp $2 4.2.0-6)" -lt 0 ]; then
        rm /var/lib/pacman/db.lck &> /dev/null
        pacman-db-upgrade
        pamacupr=$(ps -e | grep pamac-updater)
        pamacmgr=$(ps -e | grep pamac-manager)
        if [ ! -z "$pamacupr" ] || [ ! -z "$pamacmgr" ]; then
            rm /var/lib/pacman/db.lck &> /dev/null
            pacman -S --noconfirm manjaro-system
            pacman -S --noconfirm pamac
            nohup sleep 4s >/dev/null 2>&1 && DISPLAY=:0.0 nohup pamac-updater >/dev/null 2>&1 &
            killall pamac-updater &> /dev/null
            killall pamac-manager &> /dev/null
            killall pamac-tray &> /dev/null
        fi
    fi
}

post_install() {
    _check_pubring
}

_check_pubring() {
    msg "To import the data required by pacman for package verification run:"
    msg "\`pacman-key --init; pacman-key --populate archlinux manjaro\`"
    msg "See: https://www.archlinux.org/news/having-pacman-verify-packages"
}

_resetbackups() {
    msg "Performing one-time reset of NoUpgrade md5sums. After this reset"
    msg "you are able to remove all NoUpgrade lines of already protected"
    msg "files from pacman.conf."
    msg ""

    # path variables
    pacconf="/etc/pacman.conf"
    dbpath="/var/lib/pacman/local"

    # get a list of NoUpgrade files from the user's pacman.conf
    msg "Retrieving pacman.conf NoUpgrade list..."
    config=$(grep "^NoUpgrade" $pacconf | cut -d'=' -f2)
    # add the standard list of files, even if they are already above
    config="$config \
    etc/passwd etc/group etc/shadow etc/sudoers \
    etc/fstab etc/raidtab etc/ld.so.conf \
    etc/rc.conf etc/rc.local \
    etc/modprobe.conf etc/modules.conf \
    etc/lilo.conf boot/grub/menu.lst"

    # blank md5sum for use in sed expression
    zeroes='00000000000000000000000000000000'

    for file in $config; do
        echo " -> finding owner of /$file..."
        line=$(LC_ALL=C LANG=C pacman -Qo /$file 2>/dev/null)
        # if file is owned by a package, go find its incorrectly stored sum
        if [ ! -z "$line" ]; then
            # get the name and version of the package owning file
            name=$(echo $line | awk '{print $5}')
            version=$(echo $line | awk '{print $6}')
            # set the path to the backup array holding the md5sum
            path="$dbpath/$name-$version/files"
            # run a sed on the path to reset the line containing $file
            # NOTE: literal tab characters in sed expression after $file
            echo " -> resetting sum of /$file..."
            sed -i "s#$file [0-9a-fA-F]*#$file  $zeroes#" $path
        else
            echo " -> $file is unowned."
        fi
    done
}
